package com.xbn.xbnmall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.xbn.xbnmall.order.entity.OrderEntity;
import com.xbn.xbnmall.order.entity.OrderReturnApplyEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.order.dao.OrderItemDao;
import com.xbn.xbnmall.order.entity.OrderItemEntity;
import com.xbn.xbnmall.order.service.OrderItemService;

@RabbitListener(queues = {"helloQueue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {
    /*
    * 监听返回消息体是OrderEntity类型的
    * */
    @RabbitHandler
    public void recieveMessage(Message message, OrderEntity orderEntity, Channel channel) {
        System.out.println("接收到消息:"+message.toString());
        System.out.println("消息的真正内容:"+orderEntity.toString());
        System.out.println("消息处理完了，确认成功处理消息...");
        //参数1标识确认了那个消息,该标识是通道内按顺序自增的标识
        //参数二 表示是否批量，false代表只拒绝当前这一个消息
        //参数3表示这个消息拒收之后要不要让MQ重新放到队列中供下一次消费，如果是false消息就被丢弃
        try {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        } catch (IOException e) {
            //网络异常做处理....

            e.printStackTrace();
        }
    }
    /*
     * 监听返回消息体是OrderReturnApplyEntity类型的
     * */
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnApplyEntity orderReturnApplyEntity,
                               Channel channel) {
        System.out.println("接收到消息:"+message.toString());
        System.out.println("消息的真正内容:"+orderReturnApplyEntity.toString());
        System.out.println("消息处理完了，确认成功处理消息...");
        //参数1标识确认了那个消息,该标识是通道内按顺序自增的标识
        //参数二 表示是否批量，false代表只确认当前这一个消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            //网络异常
            e.printStackTrace();
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

}